from PySide6.QtWidgets import QDialog,QMessageBox
from sqlalchemy.orm import Session
from views.person.personbase.operationpersonbaseinfo import Ui_operationpersonbaseinfodialog
from settings.publicvar import engine
from views.person.model.person import PersonBaseInfo

class OperationPersonBaseInfo(QDialog,Ui_operationpersonbaseinfodialog):
    def __init__(self,is_state,data):
        super().__init__()
        self.setupUi(self)
        self.if_state(is_state)
        self.setFormStructure(data)
        self.savebtn.clicked.connect(self.savebtnFun)
        self.resetbtn.clicked.connect(self.resetbtnFun)
        self.deletebtn.clicked.connect(self.deleteFun)
        self.changebtn.clicked.connect(self.changeFun)

    def setFormStructure(self,data):
        if data:
            self.idx.setText(data[0])
            self.name.setText(data[1])
            self.IDCard.setText(data[2])
            self.iphone.setText(data[3])

    def if_state(self,is_state):
        if is_state == 'add':
            self.changebtn.hide()
            self.deletebtn.hide()
        elif is_state == 'delete':
            self.titlelable.setText('删除人员基本信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.resetbtn.hide()
        elif is_state == 'change':
            self.titlelable.setText('修改人员基本信息')
            self.savebtn.hide()
            self.deletebtn.hide()
        else:
            self.titlelable.setText('查看人员基本信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.deletebtn.hide()
            self.resetbtn.hide()

    def savebtnFun(self):
        name= self.name.text().strip()
        IDCard = self.IDCard.text().strip()
        iphone = self.iphone.text().strip()
        if name == '' or IDCard == '' or iphone == '':
            QMessageBox.critical(None, '添加失败', '以上信息不能为空！')
        else:
            with Session(engine) as session,session.begin():
                try:
                    session.query(PersonBaseInfo).filter(PersonBaseInfo.IDcard==IDCard).one()
                    QMessageBox.critical(None, '添加失败', '已经存在该人员信息！%s'%IDCard)
                except:
                    personbase = PersonBaseInfo(name=name,IDcard=IDCard,iphone=iphone)
                    session.add(personbase)
                    QMessageBox.critical(None, '添加成功', '恭喜%s添加成功！'%name)
                self.close()

    def resetbtnFun(self):
        self.name.setText('')
        self.IDCard.setText('')
        self.iphone.setText('')

    def deleteFun(self):
        idx = self.idx.text().strip()
        name = self.name.text().strip()
        with Session(engine) as session,session.begin():
            session.query(PersonBaseInfo).filter(PersonBaseInfo.id == idx).delete()
            QMessageBox.critical(None,'成功','%s删除成功！'%name)
        self.close()

    def changeFun(self):
        idx = self.idx.text().strip()
        name = self.name.text().strip()
        IDcard = self.IDCard.text().strip()
        iphone = self.iphone.text().strip()
        with Session(engine) as session,session.begin():
            person = session.query(PersonBaseInfo).filter(PersonBaseInfo.id == idx).one()
            person.name = name
            person.IDcard = IDcard
            person.iphone = iphone
            QMessageBox.information(None,'成功','%s修改成功！'%name)
        self.close()
